{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 独立使用django model（一）\n",
    "# Django Shell\n",
    "# 项目根目录中打开Django Shell : python manage.py shell\n",
    "# import django\n",
    "# django.setup()  # 装载Django\n",
    "\n",
    "# 独立使用django model（二）\n",
    "# python ×××.py or Jupyter Notebook\n",
    "# 将脚本放置在项目根目录下jupyter_notebook文件中比较方便\n",
    "import sys\n",
    "import os\n",
    "import json\n",
    "import requests\n",
    "from django.core.files import File\n",
    "from django.core.files.base import ContentFile\n",
    "# 外部脚本链接django项目\n",
    "# 添加环境变量\n",
    "'''\n",
    "print(os.path.abspath('__file__'))\n",
    "print(os.path.dirname(os.path.abspath('__file__')))\n",
    "print(os.path.dirname(os.path.dirname(os.path.abspath('__file__'))))\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis/jupyter_notebook/__file__\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis/jupyter_notebook\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis\n",
    "'''\n",
    "project = os.path.dirname(os.getcwd())  # get current work directory\n",
    "sys.path.append(project)\n",
    "sys.path.append(os.path.join(project, 'rmis'))\n",
    "# sys.path.append已设置临时环境变量，可以直接调用其中的文件，脚本是外部脚本，只是放在了项目当中而已\n",
    "os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')\n",
    "# 相关数据库配置等都在settings.py文件中\n",
    "# 导入并装载django\n",
    "import django\n",
    "django.setup()\n",
    "# 脚本正文"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 周月季年\n",
    "# 进货间隔[5,10,15]，订单数[1,3,5]\n",
    "purchase_day_delta = [5,10,15]\n",
    "invoice_num = [1,3,5]\n",
    "\n",
    "# 订单必要字段id  fk_store  fk_clerk  created\n",
    "from organization.models import Store\n",
    "from user.models import Clerk\n",
    "from invoice.models import Purchase\n",
    "\n",
    "from datetime import date, datetime, timedelta\n",
    "import random\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "stores = Store.objects.filter(type='wholesaler')\n",
    "store_id_list = [store.id for store in stores]\n",
    "\n",
    "clerks = Clerk.objects.all()\n",
    "clerk_id_list = [clerk.id for clerk in clerks]\n",
    "\n",
    "# print(good_id_list, store_id_list, clerk_id_list)\n",
    "\n",
    "\n",
    "# 进货日期\n",
    "from_date = date(2017,1,1)\n",
    "to_date = date(2019,5,18)\n",
    "purchase_days = [from_date]\n",
    "for i in range(0,100):\n",
    "    #print(purchase_days[-1])\n",
    "    purchase_days.append(\n",
    "        purchase_days[-1] + timedelta(days=random.choice(purchase_day_delta))\n",
    "    )\n",
    "\n",
    "# 进货单\n",
    "purchases = []\n",
    "for day in purchase_days:\n",
    "    for i in range(0, random.choice(invoice_num) ):  # 随机决定当天有多少笔进货单\n",
    "        purchases.append({\n",
    "            'created': day,\n",
    "            'fk_store': random.choice(store_id_list),\n",
    "            'fk_clerk': random.choice(clerk_id_list),\n",
    "        })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以直接用脚本保存了，MyModel(**()) \n",
    "# 也可以生成excel文件后导入数据库\n",
    "df = pd.DataFrame.from_dict(purchases)\n",
    "df.to_excel(\"./excel/invoice.purchase.20170101.20190518.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
